package com.jhws.sys.params.controller;

import com.jhws.sys.params.bo.SysParam;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.sys.params.bo.SysParamConstant;
import com.jhws.sys.params.service.SysParamService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by Alex Tan on 2017/6/19.
 *
 * 系统参数表相关,用于存一些key-value
 */
@Controller
@RequestMapping("/params")
@RequiresRoles("admin")
public class SysParamCtrl {

    @Resource
    SysParamService sysParamService;


    @RequestMapping("save.x")
    @ResponseBody
    JsonRespData save(SysParam param) throws Exception{
        sysParamService.save(param);
        return new JsonRespData("",1,"添加成功!");
    }

    @RequestMapping("query.x")
    @ResponseBody
    JsonRespData query(@RequestParam Map param){
        String where = "1=1";
        if(StringUtils.isNotEmpty((String)param.get("parent")) && param.get("parent").equals("other"))
            where += " and parent is NULL";
        else if(StringUtils.isNotEmpty((String)param.get("parent")))
            where += " and parent = '"+param.get("parent")+"'";

        return new JsonRespData("",1,sysParamService.findParamsPageByWhere(where));
    }

    @RequestMapping("getTypes.x")
    @ResponseBody
    JsonRespData getTypes(){
        return new JsonRespData("",1,SysParamConstant.paramTypes);
    }


    @RequestMapping("delete.x")
    @ResponseBody
    JsonRespData delete(String name) {
        sysParamService.delete(name);
        return new JsonRespData("",1,"删除成功!");
    }
}
